package at.im.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import at.im.utils.Box;
import at.im.utils.Entity;
import at.im.utils.EntityState;
import at.im.utils.Game;
import at.im.utils.GameBox;
import at.im.utils.GameBoxLayer;
import at.im.utils.GameBoxState;
import at.im.utils.GameEntity;
import at.im.utils.Sprite;
import at.im.utils.Tileset;
import at.im.utils.User;

public class SQLRead 
{
	public static ArrayList<Object> read(Connection con, int type, ResultSet rs, String searchstatement)
	{
		switch(type)
		{
			case 0: 	return readFromTable(con, rs, new Box(), searchstatement);
			case 1: 	return readFromTable(con, rs, new Entity(), searchstatement);
			case 2: 	return readFromTable(con, rs, new EntityState(), searchstatement);
			case 3: 	return readFromTable(con, rs, new Game(), searchstatement);
			case 4: 	return readFromTable(con, rs, new GameBox(), searchstatement);
			case 5: 	return readFromTable(con, rs, new GameBoxLayer(), searchstatement);
			case 6: 	return readFromTable(con, rs, new GameBoxState(), searchstatement);
			case 7: 	return readFromTable(con, rs, new GameEntity(), searchstatement);
			case 8: 	return readFromTable(con, rs, new Sprite(), searchstatement);
			case 9: 	return readFromTable(con, rs, new Tileset(), searchstatement);
			case 10:	return readFromTable(con, rs, new User(), searchstatement);
			default:	return null;
		}
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, User user, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM user WHERE userId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	        	user.setId(rs.getInt(1));
	        	user.setName(rs.getString(2));
	        	user.setEmail(rs.getString(3));
	        	user.setPassword(rs.getString(4));
	        	user.setTimeStamp(rs.getString(5));
	            list.add(user);
	            user = new User();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, Tileset tileset, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM tileset WHERE tilesetId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	        	tileset.setTilesetId(rs.getInt(1));
	        	tileset.setUserId(rs.getInt(2));
	        	tileset.setName(rs.getString(3));
	        	tileset.setPublic(rs.getBoolean(4));
	        	tileset.setTimeStamp(rs.getString(5));
	            list.add(tileset);
	            tileset = new Tileset();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, Sprite sprite, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM sprite WHERE spriteId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	        	sprite.setSpriteId(rs.getInt(1));
	        	sprite.setTilesetId(rs.getInt(2));
	        	sprite.setImage(rs.getString(3));
	        	sprite.setFrames(rs.getInt(4));
	        	sprite.setFPS(rs.getInt(5));
	        	sprite.setTimeStamp(rs.getString(6));
	            list.add(sprite);
	            sprite = new Sprite();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, GameEntity gameentity, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM gameentity WHERE gameEntityId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	        	gameentity.setGameEntityId(rs.getInt(1));
	        	gameentity.setGameId(rs.getInt(2));
	        	gameentity.setEntityId(rs.getInt(3));
	        	gameentity.setName(rs.getString(4));
	        	gameentity.setPosX(rs.getInt(5));
	        	gameentity.setPosY(rs.getInt(6));
	        	gameentity.setTimeStamp(rs.getString(7));
	            list.add(gameentity);
	            gameentity = new GameEntity();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, GameBoxState gameboxstate, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM gameboxstate WHERE gameBoxStateId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	        	gameboxstate.setGameBoxStateId(rs.getInt(1));
	        	gameboxstate.setGameBoxId(rs.getInt(2));
	        	gameboxstate.setBoxId(rs.getInt(3));
	        	gameboxstate.setName(rs.getString(4));
	        	gameboxstate.setVisibility(rs.getBoolean(5));
	        	gameboxstate.setTimeStamp(rs.getString(6));
	            list.add(gameboxstate);
	            gameboxstate = new GameBoxState();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, GameBoxLayer gameboxlayer, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM gameboxlayer WHERE gameBoxLayerId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	        	gameboxlayer.setGameboxlayerId(rs.getInt(1));
	        	gameboxlayer.setGameId(rs.getInt(2));
	        	gameboxlayer.setTimeStamp(rs.getString(3));
	            list.add(gameboxlayer);
	            gameboxlayer = new GameBoxLayer();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, GameBox gamebox, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM gamebox WHERE gameBoxId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	        	gamebox.setGameBoxId(rs.getInt(1));
	        	gamebox.setGameBoxLayerId(rs.getInt(2));
	        	gamebox.setDefaultState(rs.getInt(3));
	        	gamebox.setName(rs.getString(4));
	        	gamebox.setPosX(rs.getInt(5));
	        	gamebox.setPosY(rs.getInt(6));
	        	gamebox.setTimeStamp(rs.getString(7));
	            list.add(gamebox);
	            gamebox = new GameBox();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, Game game, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM game WHERE gameId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	            game.setGameId(rs.getInt(1));
	            game.setUserId(rs.getInt(2));
	            game.setTilesetId(rs.getInt(3));
	            game.setForegroundlayerId(rs.getInt(4));
	            game.setCenterlayoutId(rs.getInt(5));
	            game.setBackgroundlayerId(rs.getInt(6));
	            game.setBackgroundImage(rs.getString(7));
	            game.setName(rs.getString(8));
	            game.setTimeStamp(rs.getString(9));
	            list.add(game);
	            game = new Game();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, EntityState entitystate, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM entitystate WHERE entityStateId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	            entitystate.setEntityStateId(rs.getInt(1));
	            entitystate.setEntityId(rs.getInt(2));
	            entitystate.setSpriteId(rs.getInt(3));
	            entitystate.setName(rs.getString(4));
	            entitystate.setVisibility(rs.getBoolean(5));
	            entitystate.setTimeStamp(rs.getString(6));
	            list.add(entitystate);
	            entitystate = new EntityState();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, Entity entity, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM entity WHERE entityId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	            entity.setId(rs.getInt(1));
	            entity.setTilesetId(rs.getInt(2));
	            entity.setDefaultState(rs.getInt(3));
	            entity.setName(rs.getString(4));
	            entity.setTilesetId(rs.getShort(5));
	            list.add(entity);
	            entity = new Entity();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
	
	private static ArrayList<Object> readFromTable(Connection con, ResultSet rs, Box box, String searchstatement)
	{
		ArrayList<Object> list = new ArrayList<Object>();
		try 
		{
			PreparedStatement pst = con.prepareStatement("SELECT * FROM box WHERE boxId = " + searchstatement);
			rs = pst.executeQuery();

	        while (rs.next()) 
	        {
	            box.setId(rs.getInt(1));
	            box.setTilesetId(rs.getInt(2));
	            box.setSpriteId(rs.getInt(3));
	            box.setName(rs.getString(4));
	            box.setTimeStamp(rs.getString(5));
	            list.add(box);
	            box = new Box();
	        }
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
        return list;
	}
}
